package cn.tedu.web.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.tedu.web.service.IUserService;

@Controller
public class IndexController {
    @Resource
    private IUserService service;

    /**
     * 首页
     */
    @RequestMapping(path = { "", "/", "index.html", "/index.html" })
    public ModelAndView index(HttpSession session) {
        ModelAndView view = new ModelAndView("index");
        // 从 session 中获取当前登录的用户的ID
        Integer uid = (Integer) session.getAttribute("UID");
        // 从数据库中查询该用户的昵称
        String nickname = service.findNickNameById(uid);
        // 将该用户的昵称传递到 jsp 中
        view.addObject("nickname", nickname);
        return view;
    }

    @RequestMapping(path = { "login", "login.html" })
    public String login(HttpSession session) {
        // 从 session 中获取当前登录的用户的ID
        Integer uid = (Integer) session.getAttribute("UID");
        if (uid != null && uid > 0) {
            // 如果会话中已经存在用户的信息，则跳转到首页
            return "redirect:/";
        }
        return "login";
    }

    @RequestMapping(path = { "about", "about.html" })
    public String about() {
        return "about";
    }

    @RequestMapping(path = { "cart", "cart.html" })
    public String cart() {
        return "cart";
    }

    @RequestMapping(path = { "checkout", "checkout.html" })
    public String checkout() {
        return "checkout";
    }

    @RequestMapping(path = { "contact", "contact.html" })
    public String contact() {
        return "contact";
    }

    @RequestMapping(path = { "index-2", "index-2.html" })
    public String index2() {
        return "index-2";
    }

    @RequestMapping(path = { "news", "news.html" })
    public String news() {
        return "news";
    }

    @RequestMapping(path = { "news-details", "news-details.html" })
    public String newsDetails() {
        return "news-details";
    }

    @RequestMapping(path = { "product-details", "product-details.html" })
    public String productDetails() {
        return "product-details";
    }

    @RequestMapping(path = { "products", "products.html" })
    public String products() {
        return "products";
    }
}
